#include "StdAfx.h"
#include "CusServ.h"
#include "MsgSpliter.h"
#include "UtilService.h"
#include "Util.h"

bool CusServ::UserReg(char szAccount[], char szPasswd[])
{
	MSG_USER_REG regMsg;
	strcpy(regMsg.szAccount, szAccount);
	strcpy(regMsg.szPasswd, szPasswd);

	SendMessage(&regMsg, m_sock);

	MSG_BASE* pMsg = NULL;
	while ((pMsg = m_splitter.ExtractMsg()) == NULL)
	{
		int nRecv = m_splitter.RecvData(m_sock);
		if (nRecv <= 0)
			throw CException("Connection terminated");
	}

	if (pMsg->nMsgType != MSGTYPE_SUCCESS_REPLY)
	{
		delete pMsg;
		throw CException("Message type error in CusServ::UserReg");
	}

	MSG_SUCCESS_REPLY* pSucMsg = (MSG_SUCCESS_REPLY*)pMsg;
	bool bRet = pSucMsg->bSuccess;
	delete pSucMsg;

	return bRet;
}

bool CusServ::UserAuth(char szAccount[], char szPasswd[])
{
	MSG_USER_AUTH authMsg;
	strcpy(authMsg.szAccount, szAccount);
	strcpy(authMsg.szPasswd, szPasswd);

	SendMessage(&authMsg, m_sock);

	MSG_BASE* pMsg = NULL;
	while ((pMsg = m_splitter.ExtractMsg()) == NULL)
	{
		int nRecv = m_splitter.RecvData(m_sock);
		if (nRecv <= 0)
			throw CException("Connection terminated");
	}

	if (pMsg->nMsgType != MSGTYPE_SUCCESS_REPLY)
	{
		delete pMsg;
		throw CException("Message type error in CusServ::UserAuth");
	}

	MSG_SUCCESS_REPLY* pSucMsg = (MSG_SUCCESS_REPLY*)pMsg;
	bool bRet = pSucMsg->bSuccess;
	delete pSucMsg;

	return bRet;
}

void CusServ::UserSelRoute(IN MSG_ROUTE_SEL& sel, OUT vector<ROUT_ENTRY>*& pVecRes)
{
	SendMessage(&sel, m_sock);

	MSG_BASE* pMsg = NULL;
	while ((pMsg = m_splitter.ExtractMsg()) == NULL)
	{
		int nRecv = m_splitter.RecvData(m_sock);
		if (nRecv <= 0)
			throw CException("Connection terminated");
	}

	if (pMsg->nMsgType != MSGTYPE_ROUTE_REPLY)
	{
		delete pMsg;
		throw CException("Message type error in CusServ::UserSelRoute");
	}

	pVecRes = new vector<ROUT_ENTRY>;
	MSG_ROUTE_REPLY* pReplyMsg = (MSG_ROUTE_REPLY*)pMsg;
	for (int i = 0; i < pReplyMsg->nEntryNum; i++)
		pVecRes->push_back(pReplyMsg->entries[i]);
	delete pReplyMsg;
}

bool CusServ::UserReserve(int nRouteId, CABIN_TYPE nCabTp)
{
	MSG_CUST_RESERV resvMsg;
	resvMsg.nRouteNum = nRouteId;
	resvMsg.nCabTp = nCabTp;

	SendMessage(&resvMsg, m_sock);

	MSG_BASE* pMsg = NULL;
	while ((pMsg = m_splitter.ExtractMsg()) == NULL)
	{
		int nRecv = m_splitter.RecvData(m_sock);
		if (nRecv <= 0)
			throw CException("Connection terminated");
	}

	if (pMsg->nMsgType != MSGTYPE_SUCCESS_REPLY)
	{
		delete pMsg;
		throw CException("Message type error in CusServ::UserReserves");
	}

	MSG_SUCCESS_REPLY* pSucMsg = (MSG_SUCCESS_REPLY*)pMsg;
	bool bRet = pSucMsg->bSuccess;
	delete pSucMsg;

	return bRet;
}

void CusServ::ViewOrders(vector<ORDER_ENTRY>*& pVecRes)
{
	MSG_VIEW_ORDER_REQUEST viewMsg;
	SendMessage(&viewMsg, m_sock);

	MSG_BASE* pMsg = NULL;
	while ((pMsg = m_splitter.ExtractMsg()) == NULL)
	{
		int nRecv = m_splitter.RecvData(m_sock);
		if (nRecv <= 0)
			throw CException("Connection terminated");
	}

	if (pMsg->nMsgType != MSGTYPE_VIEW_ORDER_CONTENT)
	{
		delete pMsg;
		throw CException("Message type error in CusServ::ViewOrders");
	}

	pVecRes = new vector<ORDER_ENTRY>;
	MSG_VIEW_ORDER_CONTENT* pOrderMsg = (MSG_VIEW_ORDER_CONTENT*)pMsg;
	for (int i = 0; i < pOrderMsg->nEntryNum; i++)
		pVecRes->push_back(pOrderMsg->orders[i]);

	delete pOrderMsg;
}

bool CusServ::CancelOrder(int nOrderId)
{
	MSG_CANCEL_ORDER cancelMsg;
	cancelMsg.nReservId = nOrderId;

	SendMessage(&cancelMsg, m_sock);

	MSG_BASE* pMsg = NULL;
	while ((pMsg = m_splitter.ExtractMsg()) == NULL)
	{
		int nRecv = m_splitter.RecvData(m_sock);
		if (nRecv <= 0)
			throw CException("Connection terminated");
	}

	if (pMsg->nMsgType != MSGTYPE_SUCCESS_REPLY)
	{
		delete pMsg;
		throw CException("Message type error in CusServ::CancelOrder");
	}

	MSG_SUCCESS_REPLY* pSucMsg = (MSG_SUCCESS_REPLY*)pMsg;
	bool bRet = pSucMsg->bSuccess;
	delete pSucMsg;

	return bRet;
}